package com.example.fastmybatis.dao;

import com.example.fastmybatis.entity.TUser;
import com.example.fastmybatis.dao.provider.TUserSqlProvider;
import com.gitee.fastmybatis.core.mapper.CrudMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

public interface TUserMapper extends CrudMapper<TUser,Integer> {


    //mybatis有两种方式操作数据库，一个是注解，另外一个是xml文件
    //此次测试我们全部使用注解的形式，方法后面接的..ForMybatis都是以mybatis方式测试的
    @Insert({
            "insert into t_user(username,state,isdel,remark,add_time,money,left_money)",
            " values(#{username},#{state},#{isdel},#{remark},#{addTime},#{money},#{leftMoney})"
    })
    int addUserForMybatis(TUser user);


    @Delete({
            "delete from t_user where id = #{id}"
    })
    int deleteUserForMybatis(@Param("id") int id);


    int updateUserForMybatis(TUser user);

    @UpdateProvider(type = TUserSqlProvider.class,method = "updateUser")
    int updateUserForMybatis(Map<String,Object> user);

    @Select({
            "select id,username,state,isdel,remark,add_time,money,left_money from t_user where id = #{id}"
    })
    @Results({
            @Result(column = "id",jdbcType = JdbcType.INTEGER,property = "id"),
            @Result(column = "username",jdbcType = JdbcType.VARCHAR,property = "id"),
            @Result(column = "state",jdbcType = JdbcType.INTEGER,property = "id"),
            @Result(column = "isdel",jdbcType = JdbcType.INTEGER,property = "id"),
            @Result(column = "remark",jdbcType = JdbcType.LONGNVARCHAR,property = "id"),
            @Result(column = "add_time",jdbcType = JdbcType.DATE,property = "id"),
            @Result(column = "money",jdbcType = JdbcType.DECIMAL,property = "id"),
            @Result(column = "left_money",jdbcType = JdbcType.FLOAT,property = "id")
    }
    )
    TUser getUserForMybatis(@Param("id") int id);

    @SelectProvider(type = TUserSqlProvider.class,method = "getOrderUsers")
    List<TUser> getUsersForOrder();
}
